<template>
  <ych-modal
    :fullscreen="false"
    size="middle"
    :visible="visible"
    :title="$t('m.system.user.other.dept_position_label')"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-row :gutter="10">
      <a-col :md="12" :sm="24" style="margin-bottom: 20px">
        <h3>{{$t('m.system.user.other.dept_label')}}</h3>
        <a-tree
          :replaceFields="replaceFields"
          multiple
          treeCheckable="tree"
          checkable
          :checkedKeys="checkedDepKeys"
          allowClear="true"
          :checkStrictly="true"
          @check="onCheck"
          :dropdownStyle="{maxHeight:'300px',overflow:'auto'}"
          :treeData="departTree"
          placeholder=""
        >
        </a-tree>
      </a-col>
      <a-col :md="12" :sm="24">
        <h3>{{$t('m.system.user.other.position_label')}}</h3>
        <sys-position-comp :positionId="positionId" ref="positionComp"></sys-position-comp>
      </a-col>
    </a-row>
  </ych-modal>
</template>

<script>

import { getAction } from '@/api/manage'
import userModal from './UserModal'
import SysPositionComp from './SysPositionComp'
import YchConstant from '@/api/YchConstant'

export default {
  name: 'DepartWindow',
  components: {
    userModal,
    SysPositionComp
  },
  data() {
    return {
      title: this.$t('m.common.label.operate'),
      visible: false,
      userId: '', // 存储用户id
      positionId:'',//职务id
      modelName: '',//父控件名称
      departList: [], // 存储部门信息
      positionList: [], // 存储职务信息
      checkedDepKeys: [], // 存储选中的部门id
      departTree: [],
      confirmLoading: false,
      url: {
        getUserId: YchConstant.PREFIX_SYSTEM + '/user/generateUserId', // 引入生成添加用户情况下的url
        queryAllTreeList: YchConstant.PREFIX_SYSTEM + '/sysOrg/queryAllTreeList'
      },
      replaceFields: {
        children: 'children', title: 'orgName', key: 'id'
      }
    }
  },
  methods: {
    add(userId, depId,positionId,modelName) {
      this.userId = userId
      this.modelName = modelName
      if(positionId){
        this.positionId = positionId
      }
      if(depId){
        this.checkedDepKeys = []
        this.checkedDepKeys.push(depId)
      }
      this.queryDepartTree()
      this.visible = true
    },
    handleSubmit() {
      if (this.departList.length == 0) {
        this.messageUtil.warning(this.$t('m.system.user.verify.confirmdept'))
        return
      }
      this.positionList = this.$refs.positionComp.selectionRows
      if (this.userId == null) {
        this.confirmLoading = true
        getAction(this.url.getUserId).then((res) => {
          if (res.success) {
            this.ok(res.result)
          }
        }).finally(() => {
          this.confirmLoading = false
        })
      } else {
        this.ok(this.userId)
      }
    },
    ok(userId) {
      const formData = {
        userId: userId,
        departIdList: this.departList,
        positionList: this.positionList,
        modelName: this.modelName
      }
      this.$emit('ok', formData)
      this.handleCancel()
    },
    handleCancel() {
      this.visible = false
      this.checkedDepKeys = []
      this.departList = []
      this.positionList = []
      this.departTree = []
    },
    onCheck(checkedKeys, info) {
      const that = this
      const keys = checkedKeys.checked.filter(id => this.checkedDepKeys.indexOf(id) == -1)
      this.checkedDepKeys = []
      this.departList = []
      info.checkedNodes.forEach(item => {
        if (keys.indexOf(item.key) != -1) {
          that.checkedDepKeys.push(item.key)
          that.departList.push({ depId: item.key, depName: item.data.props.orgName })
        }
      })
    },
    queryDepartTree() {
      getAction(this.url.queryAllTreeList).then((res) => {
        if (res.success) {
          this.departTree = res.result
          if(this.checkedDepKeys.length>0){
            this.getTreeNodeByKey(res.result,this.checkedDepKeys[0])
          }
        }
      })
    },
    getTreeNodeByKey(treeData,id){
      let dataList = treeData.filter(item=>item.id==id)
      if(dataList.length>0){
        this.departList = []
        this.departList.push({ depId: dataList[0].id, depName: dataList[0].orgName })
      }else{
        treeData.map(item=>{
          if(item.children){
            this.getTreeNodeByKey(item.children,id)
          }
        })
      }
    },
  }
}
</script>

<style lang="less" scoped>
.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}
</style>
